//
//  PKRadioScrollerView.m
//  PianKe
//
//  Created by lanou on 15/10/4.
//  Copyright (c) 2015年 lanou. All rights reserved.
//

#import "PKRadioScrollerView.h"
#import <UIImageView+WebCache.h>
#import "PKCarousel.h"
#import "PKhotlist.h"
@interface PKRadioScrollerView()<UIScrollViewDelegate,UIGestureRecognizerDelegate>

@end

@implementation PKRadioScrollerView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
//-(NSMutableArray *)dataSourceArray
//{
//    if (_dataSourceArray==nil) {
//        _dataSourceArray=[NSMutableArray arrayWithCapacity:0];
//    }
//    return _dataSourceArray;
//}
-(instancetype)initWithFrame:(CGRect)frame
{
    self=[super initWithFrame:frame];
    if (self) {
        _welcomeScrollerView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 200)];
        _welcomeScrollerView.contentSize=CGSizeMake(SCREEN_WIDTH*3, 0);
        _welcomeScrollerView.delegate=self;
#warning 把轮播图设置为整页滑动
        _welcomeScrollerView.pagingEnabled=YES;
        for (int i=0; i<3; i++) {
            UIImageView *imageView=[[UIImageView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH*i, 0, SCREEN_WIDTH,200)];
//            imageView.backgroundColor=[UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0  blue:arc4random()%256/255.0  alpha:1.0];
//            NSDictionary *dict =self.dataSourceArray[i];
//           
//            [imageView sd_setImageWithURL:[NSURL URLWithString:dict[@"img"]]];
            [_welcomeScrollerView addSubview:imageView];
        }
        [self addSubview:_welcomeScrollerView];
        _pageController=[[UIPageControl alloc]initWithFrame:CGRectMake(300,170, 60, 30)];
      
                _pageController.numberOfPages=3;
          _pageController.currentPage=1;
        [_pageController addTarget:self action:@selector(dotapPageController:) forControlEvents:UIControlEventValueChanged];
        _pageController.pageIndicatorTintColor=[UIColor grayColor];
        _pageController.currentPageIndicatorTintColor=[UIColor redColor];
        [self addSubview:_pageController];
        _firstButton=[UIButton buttonWithType:UIButtonTypeSystem];
        _firstButton.frame=CGRectMake(5, 205,SCREEN_WIDTH/3-10 , 90);
        _firstButton.backgroundColor=[UIColor greenColor];
        [self addSubview:_firstButton];
        _secondButton=[UIButton buttonWithType:UIButtonTypeSystem];
        _secondButton.backgroundColor=[UIColor greenColor];
        _secondButton.frame=CGRectMake(SCREEN_WIDTH/3, 205, SCREEN_WIDTH/3-5, 90);
        [self addSubview:_secondButton];
        _thirdButton=[UIButton buttonWithType:UIButtonTypeSystem];
        _thirdButton.frame=CGRectMake(SCREEN_WIDTH*2/3,205 , SCREEN_WIDTH/3-5, 90);
        _thirdButton.backgroundColor=[UIColor greenColor];
        [self addSubview:_thirdButton];
        
        
    }
    return self;
}
#warning 点击pageController对ScrollerView的偏移量进行改变
-(void)dotapPageController:(UIPageControl *)pageControl
{
    NSInteger pageNumber=pageControl.currentPage;
    CGFloat pianyiliang =pageNumber * SCREEN_WIDTH;
    _welcomeScrollerView.contentOffset=CGPointMake(pianyiliang, 0);
    
    
}
#warning 通过调用set方法传过来的datasourceArray对ScrollerView里面的iamgeView进行赋值
-(void)setDataSourceArray:(NSMutableArray *)dataSourceArray
{
    _dataSourceArray=dataSourceArray;

    UIScrollView *scrollerView=(UIScrollView *)self.subviews[0];
    for (int i =0; i<scrollerView.subviews.count; i++) {
        UIImageView *imageView =(UIImageView *) scrollerView.subviews[i];
       PKCarousel *carousel =dataSourceArray[i];
        [imageView sd_setImageWithURL:[NSURL URLWithString:carousel.img]];
        
    }
    for (NSInteger i=0; i<self.welcomeScrollerView.subviews.count; i++) {
        UITapGestureRecognizer *tapRecognized=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(jumpNextController:)];
        //        tapRecognized.numberOfTouches=1;
        //        tapRecognized.numberOfTapsRequired =1;
        tapRecognized.delegate=self;
        UIImageView *imageView=self.welcomeScrollerView.subviews[i];
        NSLog(@"usjtjksdij%ld",i);
        imageView.tag=1000+i;
        imageView.userInteractionEnabled=YES;
        [imageView addGestureRecognizer:tapRecognized];
    }

#warning 当对数据请求完并进行赋值完毕的时候添加一个定时器
    [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(nextImage) userInfo:nil repeats:YES];
}
#warning 给轮播图添加手势进行跳转

-(void)jumpNextController:(UITapGestureRecognizer *)tapRecognized
{
    NSInteger count=tapRecognized.view.tag-1000;
    PKCarousel *carousel =self.dataSourceArray[count];
    NSLog(@"%ld",count);
    NSLog(@"-------%@",carousel.url);
    if (carousel.url!=nil) {
    NSString *tingid=    [[carousel.url componentsSeparatedByString:@"/"]lastObject];
        self.successTapBlcok(tingid);
    }
}
#warning 自动跳转下一个轮播图的方法
-(void)nextImage
{
    if (_pageController.currentPage<2) {
        _pageController.currentPage++;
        [self dotapPageController:_pageController];
    }
    else if (_pageController.currentPage==2)
    {
        _pageController.currentPage=0;
        [self dotapPageController:_pageController];
        
    }
        

}
#warning 通过在解析完数据后通过属性的set方法传过来的方法对三个button进行赋值
-(void)setHotlistArray:(NSMutableArray *)hotlistArray
{
    _hotlistArray=hotlistArray;
//    UIButton *firstButton=self.subviews[2];
//    PKhotlist *hostlist1=hotlistArray[0];
    [_firstButton setBackgroundImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:((PKhotlist *)hotlistArray[0]).coverimg]]] forState:UIControlStateNormal];
    [_secondButton setBackgroundImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:((PKhotlist *)hotlistArray[1]).coverimg]]] forState:UIControlStateNormal];
    [_thirdButton setBackgroundImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:((PKhotlist *)hotlistArray[2]).coverimg]]] forState:UIControlStateNormal];
    [_firstButton addTarget:self action:@selector(firstButtonDetailPage:) forControlEvents:UIControlEventTouchUpInside];
    [_secondButton addTarget:self action:@selector(secondButtonDetailpage:) forControlEvents:UIControlEventTouchUpInside];
    [_thirdButton addTarget:self action:@selector(thirdButtonDetailPage:) forControlEvents:UIControlEventTouchUpInside];

}
#warning 点击第一个界面的头视图的第一个Button时跳转到第一个button的详情界面吧
-(void)firstButtonDetailPage:(UIButton *)button
{
    NSString *radioId=((PKhotlist *)self.hotlistArray[0]).radioid;
    NSString *title=((PKhotlist *)self.hotlistArray[0]).title;
    if (self.clickButtonBlock!=nil) {
        self.clickButtonBlock(radioId,title);
    }
    
}
#warning 点击第一个界面的头视图的第二个Button时跳转到第二个button的详情界面吧
-(void)secondButtonDetailpage:(UIButton *)button
{
    
    NSString *radioId=((PKhotlist *)self.hotlistArray[1]).radioid;
    NSString *title=((PKhotlist *)self.hotlistArray[1]).title;
    if (self.clickSecondButtonBlock!=nil) {
        self.clickSecondButtonBlock(radioId,title);
    }

}
#warning 点击第一个界面的头视图的第三个Button时跳转到第三个button的详情界面吧
-(void)thirdButtonDetailPage:(UIButton *)button
{
    NSString *radioId=((PKhotlist *)self.hotlistArray[2]).radioid;
    NSString *title=((PKhotlist *)self.hotlistArray[2]).title;
    if (self.clickThirdButtonBlock!=nil) {
        self.clickThirdButtonBlock(radioId,title);
    }
    
}
#warning 滑动轮播图的时候改变pageCotroller的选中的点
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    CGFloat x=scrollView.contentOffset.x;
    NSInteger pageNumber=x/SCREEN_WIDTH;
    UIPageControl *pagecontrol=self.subviews[1];
    pagecontrol.currentPage=pageNumber;
    
    
}

@end
